Closing Windows
Since Liberty Basic does not include a command to close all remaining opened windows when the user quits a program, it falls to the programmer to complete that task for them. Of course windows of the dialog_modal type force the user to close windows in reverse order usually before they can exit the program but dialog_modal windows don’t always fit the programmer’s needs.
There are a few items the programmer must take into account when opening multiple windows and they are:
There have been a number of proposed ways to accomplish these goals on the [LB Forum] recently. Two methods that can be used are described here. The first is what I call the Dedicated Variable Method and the other is the Array Method. Both are very similar. The first works in LB 3 or 4 without any changes and lends itself very well when there are only a few windows that can be opened. The second works well when there are a lot of windows that can be opened. LB 3.0x requires an If statement for every possible open window. Otherwise, when using LB 4.x, the programmer can use the handle variable to close all opened windows when quitting the main window.
Dedicated Variable
This method uses a dedicated numeric or string variable which is set to 1 or “1” when the window is opened and set to 0 or “0” when the window is closed. As an attempt is made to open a window, the variable is interrogated and if window is already opened the program just waits. When the window is opened the variable is set to 1 or “1” and when it is closed it is set to 0 or “0”.
[WindowBranchLabel] IF First$ = “1” then wait ‘If window is open do nothing. First$ is the dedicated varible for this window. ‘define window parameters ‘open the window as #child1handle ‘maybe wait ‘do something ‘etc. [CloseWindowBranch] ‘When the user closes the window or the trapclose statement goes here. First$=”0” Close #child1handle ‘wait or go somewhere else
When the user quits the main window and exits the program, each dedicated variable is interrogated and each open window is closed.
[quit] IF First$ = ”1” then close #child1handle IF Second$ = ”1” then close #child2handle ‘etc for each possible window that could be opened Close #main END
Array Method
This method uses a numeric or string array instead of a dedicated variable, but looks much the same.
DIM WindowOpenArray$(MaximumNumber) ‘The maximum number of windows that could be opened [WindowBranchLabel] IF WindowOpenArray$(1) = “1” then wait ‘if window is open do nothing ‘define window parameters ‘open the window as #child1handle ‘maybe wait ‘do something ‘etc. [CloseWindowBranch] ‘when the user closes the window or the trapclose statement goes here. WindowOpenArray$(1) = ”0” Close #child1handle ‘wait or go somewhere else
When the user quits the main window and exits the program, the array is interrogated and open windows are closed.
[quit] FOR I = 1 to MaximumNumber IF WindowOpenArray$(I) = 1 then 'Each window that could be open needs to have the window handle in the line below. var$ = word$(“#child1handle #child2handle” ,I) Close #var$ END IF NEXT Close #main END
If the programmer is working in LB 3.0X then the quit code might look like this
[quit] IF WindowOpenArray$(1) = “1” then Close #child1handle IF WindowOpenArray$(2) = “1” then Close #child2handle Close #main END
The only advantage to this method is to keep track of the open windows in one array.
Other methods
Obviously there could be many more methods. One that was also suggested was to use a [pseudo binary string] that might look like “0000010” indicating the second describe window is opened. Another might be a [true binary method] where a numeric variable that equaled 6 would, in true binary, be “00000110” indicating that windows 3 and 2 were open. A demo program for the last method was actually started but it was discovered that there is a “lot” of convoluted code required for either of these methods. In the last case, there were two functions and two subroutines required and at least 4 lines of code for each window. At that point, it appeared there was a lot of disadvantage to either of these methods and no demo is presented for these cases.
A demo for the Dedicated Variable Method is included as CMWDemo1.bas and one for the Array Method is included as CMWDemo2.bas in the zipped archives of this newsletter.
Although the newsletter is copyrighted and this article may not be reproduced, the code contained in the demos is so basic that it is released into the public domain and you may use it or reproduce it without the author’s permission or accreditation. - JWB
DEMO
The files CMWDemo1.bas and CMWDemo2.bas are included in the zipped archive of this newsletter.